home *** CD-ROM | disk | FTP | other *** search
- ; autparen.mut -- Show matching open parens while typing
- ;
- ; Author: Tim Brengle
- ; largely derived from pmatch.mut by Craig Durland
- ;
- ; This rebinds the ")", "}", and "]" keys so that typing one of them causes
- ; the corresponding opening delimiter to be shown briefly. If there is no
- ; matching instance, you will hear a beep (if the sysvar (beeper) is non-zero)
- ; or see a message. The DELAY const has been chosen empirically so that I can
- ; keep typing at my normal speed on my PC-AT clone and just listen for the
- ; beep. Your mileage may vary. There should be no impact as long as you have
- ; sufficient type-ahead capacity.
- ;
- ; Problems and gotchas:
- ; In order for the regular expression to work properly with square brackets
- ; ("[" and "]"), the right bracket must appear first. Since it makes no
- ; difference for the others, I made it that way for all.
- ;
- ; Instances of the delimiters appearing as parts of strings are not handled
- ; any differently. Try removing and replacing the last ")" in this file.
- ; At least GNU Emacs doesn't handle it any better...
- ;
- ; Adding new matching delimiter pairs should be obvious, but they must be
- ; different characters; this won't work on strings, for instance.
-
- (const DELAY 1000) ; the bigger the number the longer the delay
-
- (defun
- insert-and-match (open-paren close-paren) HIDDEN
- {
- (string m)(int c)
-
- (m (concat '[' close-paren open-paren ']')) ;form regular expression
- (insert-text close-paren) ;insert delimiter
- (previous-character) ;so match won't end too soon
- (set-mark 9) ;don't clobber user's mark
- (c 1)
- (while (!= c 0) ;while not-balanced
- {
- (if (re-search-reverse m) ;find either open- or close-?
- (switch (get-matched '&') ;yes: update balance count
- close-paren (+= c 1)
- open-paren (c (- c 1))
- )
- {
- (if (== (beeper) 0) ;no: must be unbalanced
- (msg 'No matching "' open-paren '"')
- (puts "^G")
- )
- (goto-mark 9) ;back to insertion point
- (next-character)
- FALSE
- (done) ;quit
- }
- )
- })
- (update) ;all balanced so show
- (c 0)(while (!= c DELAY) (+= c 1)) ;delay loop
- (goto-mark 9) ;back to insertion point
- (next-character)
- (msg "")
- TRUE
- }
- )
-
- (defun
- insert-close-paren
- { (insert-and-match '(' ')') }
- insert-close-brace
- { (insert-and-match '{' '}') }
- insert-close-bracket
- { (insert-and-match '[' ']') }
- MAIN
- {
- (bind-to-key 'insert-close-paren' ')')
- (bind-to-key 'insert-close-brace' '}')
- (bind-to-key 'insert-close-bracket' ']')
- }
- )
-